require 'rubygems'
Gem.clear_paths
Gem.path.unshift(File.join(File.dirname(__FILE__), "gems"))

require 'rake'
require 'rake/rdoctask'
require 'rake/testtask'
require 'spec/rake/spectask'
require 'fileutils'
require 'merb-core'
require 'rubigen'
include FileUtils

# Load the basic runtime dependencies; this will include 
# any plugins and therefore plugin rake tasks.
init_env = ENV['MERB_ENV'] || 'rake'
Merb.load_dependencies(:environment => init_env)
     
# Get Merb plugins and dependencies
Merb::Plugins.rakefiles.each { |r| require r } 

#desc "start runner environment"
#task :merb_env do
#  Merb.start_environment(:environment => init_env, :adapter => 'runner')
#end

desc "Tests the specifications matching spec/**/*_spec.rb"
desc "Use rake spec pattern=**/my_test_spec.rb"
task :spec do
  
  require 'spec/rake/spectask'
  Spec::Rake::SpecTask.new(:spec_impl) do |spec|
    spec.spec_opts = ['-f', 's', '-r', 'config/spec_env']
    spec.pattern = ENV['pattern'] || 'spec/**/*'
  end
  
  Rake::Task[:spec_impl].invoke
end

desc "Creates rdoc from given dirs."
task :rdoc do
  `rdoc -U -o doc/helpers lib/pn-helpers/`
end

# task :clean_dev do
  # config_dev = File.open( 'config/database.yml' ) { |yf| YAML::load( yf ) }["development"]
  # `sudo -u postgres dropdb #{config_dev["database"]}`
  # `sudo -u postgres createdb #{config_dev["database"]}`
# end

# task :clean_test do
  # config_test = File.open( 'config/database.yml' ) { |yf| YAML::load( yf ) }["test"]
  # `sudo -u postgres dropdb #{config_test["database"]}`
  # `sudo -u postgres createdb #{config_test["database"]}`
# end

# desc "Cleans database and migrates schema"
# task :migrate => [:clean_test, :clean_dev] do
  # require 'db/schema'
  # ActiveRecord::Base.establish_connection(File.open( 'config/database.yml' ) { |yf| YAML::load( yf ) }["development"] )
  # Schema.up
  # ActiveRecord::Base.establish_connection(File.open( 'config/database.yml' ) { |yf| YAML::load( yf ) }["test"] )
  # Schema.up
# end